package com.yishusmt.mobsmt.controller;

import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.yishusmt.mobsmt.constant.SmsConstant;
import com.yishusmt.mobsmt.dto.base.Result;
import com.yishusmt.mobsmt.dto.response.Tips;
import com.yishusmt.mobsmt.utils.*;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

import java.util.Date;
import java.util.concurrent.atomic.AtomicInteger;

import static com.yishusmt.mobsmt.constant.RedisConstant.*;
import static com.yishusmt.mobsmt.constant.SmsConstant.SMS_STATUS;
import static com.yishusmt.mobsmt.enums.ResultCodeEnum.DATA_EXIST_ERROR;
import static com.yishusmt.mobsmt.enums.ResultCodeEnum.DAY_SEND_MAX;
import static com.yishusmt.mobsmt.enums.ResultCodeEnum.SEND_SUCCESS;

/**
 * 验证码发送
 *
 * @author zhangyi
 * @date 2018/8/9 16:28
 */
@RestController
@RequestMapping("/send")
public class SendCodeController {

    @Resource
    private RedisUtil redisUtil;

    /**
     * 发送用户验证码
     *
     * @param phone
     * @return
     */
    @GetMapping("/code/{phone}")
    @ApiOperation(value = "发送用户验证码", httpMethod = "GET")
    public Result sendCode(@PathVariable(value = "phone") String phone) {
        Result result = null;
        //设置一天发送次数上限
        Integer count = 0;
        //将用户当天的请求量存入
        String exceed = DAY_TIME_FLAG + phone;
        //记录用户发送次数
        String sendCount = SEND_COUNT + phone;
        //创建随机数
        String randCode = RandCodeUtil.getRandCode();
        //一天发送次数
        Integer daySendCount = redisUtil.get(sendCount) != null ? redisUtil.get(sendCount) : NOT_EXIST_INTEGER;
        if (daySendCount >= SEND_COUNT_MAX) {
            return TipsUtil.returnTips(DAY_SEND_MAX,null);
        } else {
            //发送短信给用户
            SendSmsResponse sendSmsResponse = AliyunSmsUtil.sendCodeSms(phone, randCode);
            if (redisUtil.get(phone) != null) {
                return TipsUtil.returnTips(DATA_EXIST_ERROR,null);
            } else {
                //用户接收验证码成功
                if (sendSmsResponse.getCode().equals(SMS_STATUS)) {
                    //存入缓存 有效期3分钟
                    redisUtil.set(phone, randCode, new Long(180));

                    if (redisUtil.get(sendCount) == null || redisUtil.get(exceed) == null) {
                        redisUtil.set(sendCount, count);
                        //设置有效日期
                        redisUtil.set(exceed, DateUtil.getLocalDate().split(" ")[0], new Long(60 * 60 * 12));
                    } else {
                        //用户发送短信的次数
                        int redisCount = new AtomicInteger(redisUtil.get(sendCount)).addAndGet(1);
                        redisUtil.set(sendCount, redisCount);
                        if(redisCount >= SEND_COUNT_MAX && redisUtil.get(exceed) == null){
                            redisUtil.del(sendCount);
                        }
                    }
                    result = TipsUtil.returnTips(SEND_SUCCESS,null);
                }
            }
        }
        return result;
    }
}
